package com.example.financechain.testinvoke;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.financechain.Utils.GatewayUtilss;
import com.example.financechain.domain.entity.Role;
import com.example.financechain.domain.entity.Transfer;
import com.example.financechain.service.IRoleService;
import org.hyperledger.fabric.gateway.Contract;
import org.hyperledger.fabric.gateway.Gateway;
import org.hyperledger.fabric.gateway.Network;
import org.hyperledger.fabric.gateway.Transaction;
import org.hyperledger.fabric.sdk.Peer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.EnumSet;

@SpringBootTest
public class createasset {
    @Autowired
     IRoleService iRoleService;

    @Autowired
    GatewayUtilss gatewayUtilss;

    @Test
    public void  create(){

        Role role =iRoleService.getById(14);

        try (Gateway gateway = gatewayUtilss.build(role).connect()) {
            // Get the network and chaincode
            Network network = gateway.getNetwork("energycarchannel");
            Contract contract = network.getContract("transferfinance");

            // Set endorsement policy
            EnumSet<Peer.PeerRole> roles = EnumSet.of(Peer.PeerRole.ENDORSING_PEER);
            // ...

            // Submit transaction
            Transaction transaction = contract.createTransaction("CreateCompany")
                    .setEndorsingPeers(network.getChannel().getPeers(EnumSet.of(Peer.PeerRole.ENDORSING_PEER)));
            transaction.submit("773", "1000", "Org1MSP");

            // Wait for transaction to complete and print transaction ID
            String txid = transaction.getTransactionId();
            System.out.println("txId:" + txid);

           // UpdateWrapper<Transfer> updateWrapper =new UpdateWrapper<>();
           // updateWrapper.lambda().eq(Transfer::getTransferId,transferOrder.getTransferId()).set(Transfer::getTransferTxid,txid);


        } catch (Exception e) {
            // Handle exceptions
            e.printStackTrace();

        }

    }
}
